package com.workpaper.app.plan.view;

import java.util.List;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.part.ViewPart;

import com.workpaper.app.cache.resource.ViewInfoCache;
import com.workpaper.app.consts.common.CommonViewConsts;
import com.workpaper.app.consts.format.CommonFormat;
import com.workpaper.app.consts.plan.DailyPlanViewConsts;
import com.workpaper.app.plan.actions.AddDailyPlanAction;
import com.workpaper.app.plan.actions.RefreshDailyPlanAction;
import com.workpaper.app.plan.business.DailyPlanBusiness;
import com.workpaper.app.plan.entity.DailyPlan;
import com.workpaper.app.plan.view.utils.DailyPlanManageViewUtils;

/**
 * 计划管理视图
 * @author zhouyujun
 *
 */
public class DailyPlanManageView extends ViewPart{
	private Table table;
	public DailyPlanManageView() {
	}

	@Override
	public void createPartControl(Composite parent) {
		table = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION);
		table.setHeaderVisible(true);	//表头可见
		table.setLinesVisible(true);	//显示表格线
		table.setRedraw(false);			//禁用重画，避免在添加内容的过程中实时刷新，影响运行效率
		//创建工具栏
		createToolBar();
		
		//设置表头：日期、内容、执行人、备注、操作
		createTableHeader();
		//填充数据
		fillTableData();
		table.setRedraw(true);	//重画
	}
	/**
	 * 创建工具栏
	 */
	private void createToolBar() {
		IToolBarManager toolBar = getViewSite().getActionBars().getToolBarManager();
		toolBar.add(new AddDailyPlanAction());
		toolBar.add(new RefreshDailyPlanAction(this));
	}
	
	/**
	 * 数据填充，临时数据，后续删除
	 */
	private void fillTableData() { 
		List<DailyPlan> plans = DailyPlanBusiness.getInstance().getAllDailyPlans();
		for (int i = 0; i < plans.size(); i++) {
			TableItem row = new TableItem(table, SWT.NONE);
			DailyPlan plan = plans.get(i);
			row.setText(0, Integer.toString(i + 1));
			row.setText(1, DateFormatUtils.format(plan.getPlanDate(), CommonFormat.DATE_FORMAT));
			row.setText(2, plan.getContent());
			row.setText(3, plan.getExecutor());
			row.setText(4, plan.getRemark());
			row.setData(plan.getId());
		}
		DailyPlanManageViewUtils.getInstance().addRowOperatingButtons(table);
	}
	/**
	 * 创建表头
	 */
	private void createTableHeader() {
		TableColumn orderNumColumn = new TableColumn(table, SWT.NONE);
		orderNumColumn.setText(ViewInfoCache.get(CommonViewConsts.TableHead_Order));
		orderNumColumn.setWidth(50);
		TableColumn dataColumn = new TableColumn(table, SWT.NONE);
		dataColumn.setText(ViewInfoCache.get(DailyPlanViewConsts.DailyPlanManageTableHead_Date));
		dataColumn.setWidth(120);
		TableColumn contentColumn = new TableColumn(table, SWT.NONE);
		contentColumn.setText(ViewInfoCache.get(DailyPlanViewConsts.DailyPlanManageTableHead_Content));
		contentColumn.setWidth(280);
		TableColumn personColumn = new TableColumn(table, SWT.NONE);
		personColumn.setText(ViewInfoCache.get(DailyPlanViewConsts.DailyPlanManageTableHead_Executor));
		personColumn.setWidth(80);
		TableColumn remarkColumn = new TableColumn(table, SWT.NONE);
		remarkColumn.setText(ViewInfoCache.get(DailyPlanViewConsts.DailyPlanManageTableHead_Remark));
		remarkColumn.setWidth(80);
		TableColumn operationColumn = new TableColumn(table, SWT.NONE);
		operationColumn.setText(ViewInfoCache.get(CommonViewConsts.TableHead_Operation));
		operationColumn.setWidth(127);
	}

	
	public void clearTableData() {
		DailyPlanManageViewUtils.getInstance().clearRowButtons(table);
		TableItem[] items = this.table.getItems();
		for (TableItem item : items) {
			//清理按钮
			item.dispose();
		}
	}
	
	public void refreshTableData() {
		clearTableData();
		fillTableData();
	}
	
	@Override
	public void setFocus() {
		
	}

}
